package com.speedcar.base.exception;

import com.speedcar.base.i18n.ScI18nUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 自定义异常-Base父类，细化的自定义异常，应该继承此类
 * 统一异常处理时，会根据此异常类型做判断，返回结果时，如果是自定义异常自动解析code和errorMsg返回
 *
 * @author zhangfuzeng
 * @date 2021/12/8
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Slf4j
public class SpeedCarBaseException extends RuntimeException {

    private final String code;

    private final String errorMsg;

    private Object[] args;

    public SpeedCarBaseException() {
        this.code = ErrorCode.OTHER_ERROR.getCode();
        this.errorMsg = ErrorCode.OTHER_ERROR.getI18nCode();
        this.args = new Object[0];
    }

    public SpeedCarBaseException(String errorMsg) {
        super(errorMsg);
        this.code = ErrorCode.COMMON_ERROR.getCode();
        this.errorMsg = errorMsg;
        this.args = new Object[0];
    }

    public SpeedCarBaseException(String code, String errorMsg) {
        super(errorMsg);
        this.code = StringUtils.isBlank(code) ? ErrorCode.COMMON_ERROR.getCode() : code;
        this.errorMsg = errorMsg;
        this.args = new Object[0];
    }

    public SpeedCarBaseException(ErrorCode code) {
        super(code.getI18nCode());
        this.code = code.getCode();
        this.errorMsg = code.getI18nCode();
        this.args = new Object[0];
    }

    public SpeedCarBaseException(ErrorCode code, String errorMsg) {
        super(errorMsg);
        this.code = code.getCode();
        this.errorMsg = errorMsg;
        this.args = new Object[0];
    }

    public SpeedCarBaseException(String errorMsg, Object[] args) {
        super(errorMsg);
        this.code = ErrorCode.COMMON_ERROR.getCode();
        this.errorMsg = errorMsg;
        this.args = args != null ? args : new Object[0];
    }

    public SpeedCarBaseException(String code, String errorMsg, Object[] args) {
        super(errorMsg);
        this.code = StringUtils.isBlank(code) ? ErrorCode.COMMON_ERROR.getCode() : code;
        this.errorMsg = errorMsg;
        this.args = args != null ? args : new Object[0];
    }

    public SpeedCarBaseException(ErrorCode code, Object[] args) {
        super(code.getI18nCode());
        this.code = code.getCode();
        this.errorMsg = code.getI18nCode();
        this.args = args != null ? args : new Object[0];
    }

    public SpeedCarBaseException(ErrorCode code, String errorMsg, Object[] args) {
        super(errorMsg);
        this.code = code.getCode();
        this.errorMsg = errorMsg;
        this.args = args != null ? args : new Object[0];
    }

    @Override
    public String getMessage() {
        // 如果有错误码和参数，尝试获取国际化消息
        if (StringUtils.isNotBlank(code) && args != null) {
            try {
                // 使用ScI18nUtils和args获取格式化后的国际化消息
                String i18nMessage = ScI18nUtils.get(code, args);
                if (StringUtils.isNotBlank(i18nMessage) && !i18nMessage.equals(code)) {
                    return i18nMessage;
                }
            } catch (Exception e) {
                log.warn("Failed to get i18n message for code: {}", code, e);
            }
        }

        // 如果没有获取到国际化消息，返回原始错误消息
        return StringUtils.isNotBlank(errorMsg) ? errorMsg : super.getMessage();
    }

    // 提供获取args的方法
    public Object[] getArgs() {
        return args != null ? args : new Object[0];
    }
}
